package cn.edu.nju.contact.add;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.edu.nju.contact.R;
import cn.edu.nju.contact.cropimage.CropImage;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

public class ContactAddActivity extends Activity {
	private static final int CAMERA_ACTIVITY_REQUEST = 0;
	private static final int PHOTO_CUT_REQUEST = 1;
	private static final int NFC_REQUEST = 2;

	Uri fileUri;
	Button add_image_button;
	
	Button nfc_button;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact_add);

		add_image_button = (Button) findViewById(R.id.button_add_image);
		add_image_button.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

				fileUri = getPhotoFileUri();
				intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

				startActivityForResult(intent, CAMERA_ACTIVITY_REQUEST);
			}
		});
		
		nfc_button = (Button)findViewById(R.id.contact_add_nfc);
		nfc_button.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Intent intent = new Intent(ContactAddActivity.this, ContactNFCAddActivity.class);
				startActivityForResult(intent, NFC_REQUEST);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == CAMERA_ACTIVITY_REQUEST) {
			if (resultCode == RESULT_OK) {
				startCutPhoto();
			} else if (requestCode == PHOTO_CUT_REQUEST) {
				addPhoto();
			}

		}

		if (requestCode == PHOTO_CUT_REQUEST && resultCode != RESULT_CANCELED) {
			addPhoto();
		}
	}

	private Uri getPhotoFileUri() {
		File photoStorageDir = new File(
				Environment
						.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
				"MagicTaoBao");

		if (!photoStorageDir.exists()) {
			if (!photoStorageDir.mkdirs()) {
				Log.d("MagicTaoBao", "failed to create directory");
				return null;
			}
		}

		String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
				.format(new Date());

		File photoFile = new File(photoStorageDir.getPath() + File.separator
				+ timeStamp + ".jpg");
		return Uri.fromFile(photoFile);

	}

	private void startCutPhoto() {
		Intent intent = new Intent(this, CropImage.class);

		intent.putExtra("image-path", fileUri.getPath());
		intent.putExtra("scale", true);
		startActivityForResult(intent, PHOTO_CUT_REQUEST);
	}
	
	private void addPhoto(){
		Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath());
		add_image_button.setBackgroundDrawable(new BitmapDrawable(bitmap));
	}
}
